package cn.com.klun.util;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

public class IsWorkDayUtil {

    public static boolean isworkday(String date){
        String url = "";
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        // http://api.goseek.cn/Tools/holiday?date=20170528
        url = "http://api.goseek.cn/Tools/holiday?date="+date;
        try {
            URL temp = new URL(url);
            URLConnection uc = temp.openConnection();
            uc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");
            is = temp.openStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line;
            while((line = br.readLine()) != null){
                result.append(line);
            }
            //看看返回的字符串格式
            String dateInfo = result.toString();
            JSONObject jsonObject = JSONObject.fromObject(dateInfo);
            // 返回数据：工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
            // Object code = jsonObject.get("code");
            Object data = jsonObject.get("data");
            if (!"0".equals(String.valueOf(data))){
                return false;
            }
        } catch (Exception e) {
            // logger.info("读取网页异常{}",e.getMessage());
            return false;
        } finally {
            try {
                is.close();
                isr.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


}
